Manage sticky post content (for ads purpose) via the admin panel

Hi.
I’m working on a theme that I try to make from scratch. Now I want to have two sticky post (similar to sticky post) in my theme which uses for ads purpose. So I want the site owner to have the ability to change the sticky post content from admin panel and inside of my theme options.
And I want my theme has this ability out of the box so I don’t want to use a plugin.
Is there a way to achieve this ?
Here’s what I want to have in my theme options: http://up.vbiran.ir/uploads/44767141880909010163_ads-control.jpg .
I have a menu for my theme options in admin panel right now.
In this settings page I want to have two fields for editing ads(as shown in image above). Ads must show on all blog pages, so I think sticky post is not an option. This way Those ads are easily editable and when site owner edits them, all content in ALL of blog pages changed at once and there’s no need to do anything else… Now I think my goal is more obvious. Could you please help me to achieve that ?

One solution I used was to create a category ‘sticky’ and use WP_query on all pages to find all posts with that category and a loop where I wanted to place these ‘sticky’ posts. So all I had to do was assign the category to those posts.

Another way would be to create custom post types for your ‘sticky posts’ and call that post type where you want to place the ads.

1 Like

Thanks WebMachine.
In addition to your suggestion, someone suggested me to “store ads in WordPress options with the help of Options API. And use get_option() function to print ads anywhere you want.”

Again here’s my expectation:
ads placed two or three time in each blog page, and they show up between posts.
those ads content are different from each other and must be manageable via admin panel. Meaning admin can edit and change those ads in admin panel, so ads in all blog pages affected and change to new ads content.

What you (or other kind members) specifically recommend me to do in this situation ?

Thanks.

I totally get lost on this subject.

I read about Options API and worked a little with it.

Now I can save the field’s content in the database and retrieve that using Options API.

But I can’t figure out how I can show those data in my theme. I don’t talk about get_option(), I know a little about that.

The thing is, in my index page, I want two sticky post above all of
posts that stays there in ALL index pages (so sticky post is not an
option). I don’t know how I can create those two or more posts that
stays there all the time and I totally Lost here.

When I figure out how to do this, retrieving the content from wp_option table using Options API isn’t so hard I think.

I appreciate if anyone can help me.
Thanks

I would use a custom post type like @WebMachine suggested. I wouldn’t go the category way because you’re using the “sticky posts” as ads, so you’d have to make sure not to show that category in certain places (widgets etc) and remember to block them from search results.

It would be easier, more robust, and future proof than the Options API way anyways.

Here’s how I would do it:
1) Upgrade WordPress :stuck_out_tongue: That screenshot you posted looks like it’s from 3.2 or earlier!
2) Register a new post type. Literally, all you have to do is add this to your functions.php:

add_action('init', function(){
	register_post_type('ads', array(
		'public' => true
	));
});

That’ll create a new menu in your dashboard labeled “ads” with the ability to create as many ads as you need with a Title and Content area. If in the future you need more control, adds categories or new fields. You can change the menu icon and labels by passing in different arguments (see that link). If you get stuck, just ask.

3) In your blog template, add this bit near the top somewhere:

$ads = get_posts(array(
	'posts_per_page'	=> 2,
	'post_type'		=> 'ads'
));

That’ll get the latest 2 ads and store them into $ads. If you need more control, check out get_posts. You can start to see why the custom post type way is better, as you can give your ads categories and query them in fancy ways. Anyways, to display them, add something like:

foreach($ads as $ad): ?>
	<h1><?= $ad->post_title ?></h1>
	<div class="content">
		<?= apply_filters('the_content', $ad->post_content) ?>
	</div>
<?php endforeach;

If you need to display them between posts, you can just do something like:

if($count === 3 || $count === 6)
	//show $ads[0] or $ads[1]
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.