Cant' Grab WordPress Built-in Posts Through Loop - wordpress

Using WordPress 3.7.1 I am trying to display all Regular Post on my created page lest say TestPage. Here are the steps I took to do this:
1- Generate a Custom Page Template called:Test Page and loaded by following code
2- Generate a Page Called TestPage based on Test Page Template
after updating the page I am not getting any of Post on the page while I have already generated some!
<?php
/*
Template Name: Test Page
*/
?>
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
<?php get_footer(); ?>
he abouve code actually is loading the page whit title and content of the TestPage and not by Posts!Can you please let me know why this is happening?

You must understand the difference between post and page.
A page is associated with a template. Through code, you can lists posts or other pages using this page /template.
The above code will only display the Page data
You don't want to create many pages using the same tempalte in order to show different data with the same tempalte. Instead you create posts and give them a category.
Then you can fetch these posts and display them in the same way asthe test-page.
You can either use get_posts or wp_get_recent_posts.
Or, if you really want to list pages, use wp_list_pages.
Besides that, you should look at http://codex.wordpress.org/Pages and http://codex.wordpress.org/Posts

Related

Angular + WordPress

I have a ticket sales form written in Angular. How can I insert this form on the main page of Wordpress site? May be with usingthe tag iframe or can there be better solutions for this task? Thank you for attention!
I think a best solution to this is to create a new page template (you can google it to get more accurate info), see what is page templates here.
Basically, you will create a new file on your theme root, with the prefix page-, e.g: page-tickets.php. And then the very first thing that must appear on your page is a comment,like this: // Template Name: Tickets Page. Your page will be something like this:
<?php
// Template Name: Tickets Page
get_header();
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Angular + Html codes here!
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<script>angular and js imports</script>
<?php get_footer(); ?>
With that, when you will create a new page in Pages -> New (or edit), in the right bottom side will be available an option to you to choose a page template, like this:
That's all, I hope it helps you.

How to repeat ACF Flexible Content on single pages?

Disclaimer: If you have never used ACF Flexible Content to build a site, please reserve the hate and all that - looking just for help.
I'm creating a website that's 100% modular, done with Flexible Content.
I have Pages such as:
Home, About, Services, Blog, Portfolio, Contact
All of those pages styled using Flexible Content, these are PAGES.
I have created a custom field for Services and Portfolio, and I used WP Query to display each individal item. These are CUSTOM fields. Show this field group if post type to Services. Not a page.
So when you click it, what happens you go to single-[custom-post-type].php, however I just have single.php to keep it dynamic, which works.
The problem lays that when I have showed the flexible content page templates in the Services or Portfolio, and I add few items, I need go to every single item and set the layout.
How can I do so the layout stays all the same in Service or Portfolio custom field?
Firstly create your ACF Master block, for example, a testimonial section, this would have the repeater fields inside the block etc. In this example, we will call it Testimonial Master.
Then Create another Flexible Content Field Called Testimonial Block with a checkbox that you check to display or hide.
You will now populate Testimonial Master on your selected Parent Page or on an Options page. You then should add the Testimonial Block to any page you are looking to have that block appear on.
You will then went to check that the Testimonial Block is on the page, if it is on the page then you will check if Testimonial Master has a value if it has a value then you can pull in the values from Testimonial Master.
<?php
if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) :
the_row();
if( get_row_layout() == 'testimonial_block' ):
if( get_sub_field('testimonial_block_toggle') == 'show' ):?>
<div class="testimonials">
<?php the_sub_field('testimonial_master_title', 1); ?>
</div>
<?php endif;
endif;
endwhile;
endif;
?>
The "1" is the page ID, use this if you have populated the field on a master page. Or if you are using the ACF Options Page plugin change the "1" to "'options'".
Here is some further documentation on getting values from other posts including that of repeater fields.
https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
Edit: Custom Header as per your example
Make a custom header file and add your code for that header block.
Now make an advanced custom field for a text area. For example header_text_block, set it to a WYSIWYG editor.
Now in your code just use:
<?php the_field('header_text_block');?>
This will then use the exact same header on every page you pull it into, however it will let you edit the text individually for each page.
Edit: Example of what this code looks like and does
Also I think you are very confused, there will only be 1 php/html file? you could use page.php.
Here's an example of what I am saying, 1 php file:
<?php if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) : the_row();
if( get_row_layout() == 'page_featured' ):
$image = get_sub_field('page_featured_image');
if( !empty($image) ):?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
<?php endif;
endif;
if( get_row_layout() == 'page_breadcrumbs' ):
if( !in_array( 'hide', get_sub_field('page_breadcrumbs_hide') ) ):?>
<?php if ( function_exists('yoast_breadcrumb') ) {yoast_breadcrumb('<p id="breadcrumbs">','</p>');}?>
<?php else :
endif;
endif;
endwhile;
endif;
?>
This then looks like this on the page editor:
You can then edit each section as you please and also drag and drop sections where you want them which enables easy split testing.
All the styling will be the same, just you can change the content and if you wanted you could add the ability to alter stylings pretty easily. And it only uses 1 PHP file for the template.

Wordpress - Three custom posts with the same slug

I have three posts that have the same title and address, if I am logged in it displays my posts at all, but it logs out can not see them and shows me 404 if we can somehow read these posts?
i read post with this:
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part('templates/content', 'single');
?>
<?php endwhile; // end of the loop. ?>
I think your posts are private posts. Just edit a post from them and check at the right side of your content editor. There are few options, make this post public and then click on update.
Now check, your issue got resolved.
Go through this article for more info : http://en.support.wordpress.com/posts/post-visibility

How to display list categories in wordpress integration magento

Can help somebody. I spent several hours to find solution but without results
I tried to display the list of categories on homepage wordpress blog thru following code
<?php $category = Mage::registry('wordpress_category') ?>
<?php if ($category): ?>
<?php echo $category->getId() ?>: <?php echo $category->getName() ?>
<?php endif; ?>
But the method
Mage::registry('wordpress_category')
always return null.
I found that, i should probably be using the Fishpig_Wordpress_Block_Category_View. But i dont know where i should put it.
The following code will retrieve the current category when viewing a category page in your blog:
<?php Mage::registry('wordpress_category') ?>
This is not what you need. To view a list of categories, you could create a custom collection using the following:
<?php $categories = Mage::getResourceModel('wordpress/post_category_collection') ?>
A better way would be to use the category widget block:
<block type="wordpress/sidebar_widget_categories" name="wp.categories" template="wordpress/sidebar/widget/categories.phtml" />
You can create this in PHP using the following code:
<?php echo Mage::getSingleton('core/layout')
->createBlock('wordpress/sidebar_widget_categories')
->setTemplate('wordpress/sidebar/widget/categories.phtml')
->toHtml() ?>
The above code uses the default template, however, feel free to use your own custom template.

How to retrieve all custom posts from all the categories in portfolio

I have created custom post type portfolio in wordpress. I have created categories like web-design, logo design, e-commerce, photography... etc. I have retrieved all these category as the navigation menu in portfolio template file (assigned to the portfolio page). Now I want there should be a link(view all) in the navigation menu which will retrieve all the posts from all the categories in portfolio. Basically I want a way for making default category which will retrieve all the posts. Can anybody please help me!!!
So, basically, you want to create a Custom Post Type Archive page.
A way to achieve this, as explained at WPBeginner.com, is like the following:
Create a custom page template (the PHP file could have any name you want) and Add the Template Name comment at the beginning of the file:
<?php /* Template Name: Custom Post Type Archive */ ?>
Create a custom loop:
<?php
/* Template Name: Custom Post Type Archive */
get_header();
?>
<?php
global $query_string;
query_posts($query_string . "post_type={YOUR-CUSTOM-POST-TYPE}&post_status=publish&posts_per_page=10");
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php
endwhile;
endif;
?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>
<?php get_sidebar(); ?>
<?php get_footer();?>
Create a new page and select the template you just created in the Page Attributes box
You should be able to use the archive page template to list them all then. Try going to http://yourdomain.com/portfolio and see if that brings it up. You will need to have enabled archives for the post type when you created it though. Look for 'has_archive' => true, if you created the post type manually by dropping in the code in your functions.php file. If you see that, but its set to "false", you'll need to change it to true.
You could also create a custom archive template for this post type if you wanted to. If you create a new file in your theme directory called archive-portfolio.php and then put a custom loop in there the post type will automatically target that template file when accessing the post type archive.
Then to link from your nav menu to this archive, just create a custom menu item in your menu editor with the url http://yourdomain.com/portfolio. You won't need to create any sort of "default" category if you take advantage of the post type archives.
Let me know if you have any other questions.

Resources