I have some code, namely -
<?php while (has_sub_field('services_featured_links')) {
$postObjects = get_sub_field('services_link');
if($postObjects){ ?>
<ul class="intro-menu">
<?php foreach($postObjects as $post){
setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php
}
wp_reset_postdata();
}
?>
</ul>
<?php } ?>
I have 4 post objects in my repeat fields - at the moment, utilising this code returns a lot of empty li and a tags. It also returns the parent page as a link.
I basically just need to get the permalink and title of each post object I have selected.
Any help please?
$postObjects is not what you think it is.
ACF returns the value of the field called services_link, if that's just a field then you probably have a string inside $postObjects.
If this is for a repeater block, and I'm willing to guess it is, then you need to loop through and initialise it with the_row().
I'm assuming services_featured_links is the name of your repeater, services_links is the name of the field you use for the link within that repeater, and I'm also assuming you have a services_title field. A lot of assumptions there.
My guess is you'll want something along the lines of the following:
<?php if( have_rows('services_featured_links') ){ ?>
<ul class="intro-menu">
<?php while (have_rows('services_featured_links')) { the_row();
$services_link = get_sub_field('services_link');
$services_title = get_sub_field('services_title');
<li><?php echo $services_title; ?></li>
<?php } ?>
</ul>
<?php } ?>
Related
I need the option to build a page showing all posts of a specific category.
Showing all posts of a category can be done out-of-the-box by wordpress, I know. But I need the possibility to put some information about all those posts.
I know there's a plugin called "List category posts" (http://wordpress.org/plugins/list-category-posts/). It works but it's only showing the links to the posts. I need the full posts (like they are shown on the "blog page").
If you need to "do something" to results, look at
query_posts
via http://codex.wordpress.org/Function_Reference/query_posts
Here is a sketch that I think leans towards your needs using a custom loop. This can be inserted as needed via simple logic in your template:
// this sets up the filter parameters for a category id some_cat_id sorting asc
$args = array(
'cat' => $some_cat_id,
'order' => 'ASC'
);
// The query is applied via a conditional
if($some_conditional) { // for what ever reason we use the filter+args
query_posts( $args );
// this is also an opportunity to "do stuff" before the loop/output
}
// The Loop (simple example)
while ( have_posts() ) :
the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
As a long time WP user I avoid plugins at all costs in preference of writing sustainable code. Plugins are a point of failure and some of the biggest plugin factories out there are nothing but security issues wrapped in sugar.
Custom loops via conditionals using query "filtering" is amazing and this pattern can be extended to category, search, tags, and meta key:value pairs.
Additionally, by understanding the loop the formatting and output can be controlled in a manner that is easy to sustain. Some of the plugin logic is horrid and very inefficient, so always investigate any and all plugins when performance and security are important.
Here's what I find to be the most simple way to do this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//You can change up the format below any way you'd like.
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
</li>
<?php endwhile; endif; ?>
You can add this to a theme template file and all you need to change is the category id to the category you are trying get posts from. For example if your category id is '114' and you would like to show 9 posts it would look like the following:
<?php query_posts('cat=114&showposts=9'); ?>
If you need to add more info to the posts you should consider using custom fields to do that. Check out the plugin called Advanced Custom Fields.
Here is an example of a custom field being used in a loop:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
<?php $article_link=get_post_meta($post->ID, 'article-link', true);?>
<?php if ( $article_link ) : ?>
<?php else : ?>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
In the above example, if the custom field 'article-link' has a value, then that value (a URL) is used as the href in a link instead of the permalink of the article.
Hope I have helped!
I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.
The problem I'm having is that I'm trying to use the built-in functionality of Wordpress and I can't find a way to add sort functionality.
My Scenario looks like this
The calling url is ...com/taxonomy/term
This calls up a template, the filename of which is taxonomy-taxonomyname-term.php
My template is simply the index.php template renamed and edited to contain this loop
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
This displays a list of titles but I can't find a way to control the order of the titles.
The only way I've seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.
Is there any way to add sort functionality without adding another query or is the query mandatory.
Suppose your custom fields is my_date
You can create custom query like this.
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
To use it
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
For more info http://wpengineer.com/1915/sort-posts-custom-fields/
I am using wp_list_pages to create a submenu on a page and child page.
All working fine
My menu looks like
Parent, page title is Hello
Child,
Child,
Child,
etc
I am trying to find a way to dynamically change the Page title on output.
In my example above, I would like my Parent page to display GoodBye instead of Hello.
You might wander why I don't just rename my page to Goodbye.
It is because the Page title , in my design, is displayed in 3 different format
- menu Header Hello displays Welcome (can change this via WP menu
- Page title display the correct title, ie Hello
I need my left menu to display Goodbye....
hope this makes sense for somebody
thx
Use a custom field on your page...let's call it sidebar_title.
Then, you'll need to convert your wp_list_pages code into a custom WordPress loop (there might be a way to use get_pages to do the same if you prefer that.
Here's some sidebar code to list the current page and it's child pages, replacing the_title(); with your sidebar_title if it exists. It's pretty ugly...the main point is to show you how to access custom fields.
<?php
//Get children of current page and display with custom fields.
//You will probably need to adjust this.
$args=array(
'post_parent' => $post->ID,
'post_type' => 'page',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
?>
<ul>
<?php
// Print parent with sidebar_title, if it exists
$sidebar_title = get_post_meta($post->ID, 'sidebar_title', true);
if ($sidebar_title != ''){ ?>
<li><?php echo $sidebar_title;?></li>
<?php } else { ?>
<li><?php the_title(); ?></li>
<?php } ?>
<?php
// Print each child page with sidebar_title, if it exists
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
$sidebar_title = get_post_meta($post->ID, 'sidebar_title', true);
echo $sidebar_title;
if ($sidebar_title != ''){ ?>
<li><?php echo $sidebar_title;?></li>
<?php } else { ?>
<li><?php the_title(); ?></li>
<?php } ?>
<?php endwhile; } ?>
</ul>
<?php wp_reset_query();?>
I am trying to set my WordPress homepage to a category but it only allows me to set it to either the latest posts or a static page.
Is it possible to set your homepage as a post category?
I am hoping that you know about how to set static page. So first create an empty .php file and name it whatever you like and put it along the other files (index.php, arhive.php etc).
and then enter following code
<?php
/*
* Template Name: Category based Homepage
*/
?>
<?php get_header(); ?>
<div class="main">
<?php
$cat_ID = '1'; //it should be your category ID, you can get the id of the category by going to categories and edit and then in url you can find the tag_ID.
$posts_to_show = '10'; // number of posts from the category you want to show on homepage
//query_posts("cat=$cat_ID&showposts=$posts_to_show");
$category_posts = new WP_Query("cat=$cat_ID&showposts=$posts_to_show");
//if (have_posts())
if ($category_posts->have_posts())
: $first = true;
?>
<ul class="post-list">
<?php
//while (have_posts()) : the_post();
while ($category_posts->have_posts()) : $category_posts->the_post();
if ($first)
$class = "first-in-row";
else
$class = "";
$first = !$first;
?>
<!-- Start: Post -->
<li <?php post_class($class); ?>>
<?php the_post_thumbnail(); ?>
<p class="categories"><?php the_category(", "); ?></p>
<h2><?php the_title(); ?> <?php edit_post_link(__('Edit', 'your_theme_text_domain'), '', ''); ?></h2>
<p class="post-meta"><span class="date"><?php the_time(get_option('date_format')) ?></span> <?php if (comments_open()) : ?>, <span class="comments"><?php comments_popup_link(_x('0', 'comments number', 'your_theme_text_domain'), _x('1', 'comments number', 'your_theme_text_domain'), _x('%', 'comments number', 'your_theme_text_domain')); ?></span> <?php endif; ?> <span class="author"><?php the_author() ?></span></p>
<?php the_excerpt(); ?>
<p class="more"><?php _e('Read More »» ', 'your_theme_text_domain'); ?></p>
<?php if (has_tag()): ?><p class="tags"><span><?php the_tags(""); ?></span></p><?php endif; ?>
</li>
<!-- End: Post -->
<?php endwhile; ?>
</ul>
<?php else : ?>
<h2 class="center"><?php _e('Not found', 'your_theme_text_domain'); ?></h2>
<p class="center"><?php _e('Sorry, but you are looking for something that isn\'t here.', 'your_theme_text_domain'); ?></p>
<?php
endif;
//wp_reset_query();
wp_reset_postdata();
?>
</div>
<?php get_sidebar(); //optional?>
<?php get_footer(); ?>
and replace $cat_ID and $posts_to_show to your liking. And I have used both query methods adjust it to your needs.
Hope it helps somebody who is looking for similar solution.
You can create a custom template that mimics a category page using get_posts and set a page using that template to home, but it won't be perfectly dynamic in the sense that you have to hard code the category slug or ID into that query. Assuming that you don't want to change that category often, that shouldn't be an issue. Alternatively, you could use wp_safe_redirect in a template to redirect to the category page - that would be if you want the user to be put directly on the real category page, URL and all.
I'm not sure what you mean by having your home page as a category, you mean that in your home page posts that will be displayed will be only from a certain category ?
You only need to perform a WP_Query before the loop;
$query = new WP_Query("cat=10, paged=".get_query_var('paged'));
Then use use the WP_Query object to perform the loop;
if($the_query->have_posts()):
while($the_query->have_posts()):
the_title();
the_content();
//Use all the loop function normally
endwhile;
endif;
The paged parameter is used to determine in which page you are, if you need paginantion.
Instead of using the category id, it is good to retrieve the id by the slug.
$home = get_category_by_slug('home-category-slug');
Then your query will be like this
$the_query = new WP_Query("cat=".$home->cat_ID.", paged=".get_query_var('paged'));
Yes this is possible Go to dashboard>>Setting>>Reading>>Static page Choose page from drop down and SAVE. On that page you can create your own stuff...
I am using the following code to try and display posts from only a certain category horizontally in three rows. I have the horizontal display issue figured out (using css) but with the following code it displays all posts and not posts from specific category.
<?php query_posts('showposts=5'); ?>
<?php query_posts('cat=7'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
Any help would be greatly appreciated.
You're misunderstanding some concepts in query_posts and get_posts.
query_posts is to be used inside the loop. get_pages isn't. If you want to use query_posts, you don't need to create the get_pages call. Use query_posts or get_pages to accomplish what you're trying to do.
You need to combine your category parameters in query_posts.
<?php
query_posts('showposts=5&cat=7');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
If you want to do the same logic but without The Loop, just call
$posts = get_posts('numberposts=5&offset=0&category=7').
Read the links I provided. They have all information you need to understand how to do what you need.