What I'm trying to do is convert my one page design in wordpress and i thought it would be nice to be able to edit, add and modify different part of the page in seperated pages. The one page website will be ordered by a menu (with id main).
Following the wp-codex i used get_template_part, it should work properly because it should:
Load a template part into a template (other than header, sidebar, footer)
get_header gets skipped but get_footer gets excecuted and the site is rendered incorrectly.
front-page.php
$pages = wp_get_nav_menu_items('main');
global $post;
foreach ($pages as $post) {
$post = get_post($post->object_id);
if($post->post_type != "page") continue;
setup_postdata( $post );
$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? null : $post->post_name;
get_template_part('page', $postName);
}
wp_reset_postdata();
Is this a bug? Or did I do something wrong? What is the best way to accomplish this?
if your get_template_part() is not working, then the problem may be there is no file named as page.php in theme, check for this file.
For what it's worth, I often skip a some of the WP helpers and work more directly, but not rogue. Here is a snippet from my library :
/** Get core data for a WP post/page using ID
* #param $id : wp post/page ID
* #return array
*/
function wp_get_single_post_basic($id){
$post = get_post($id, ARRAY_A);
$ret = array();
$content = $post['post_content'];
// FILTER via WP
$content = apply_filters('the_content', $post['post_content']);
$ret['title']= $post['post_title'];;
$ret['content']= $content;
$ret['link'] = $post['post_name'];
$ret['edit_link'] = 'edit';
return $ret;
}
That function breaks down the content in a very easy to manage manner and generates the edit link (build bool for that in calling script). Also formats content.
So grab and sort the IDs you want and run this in the loop over IDs for your one page. Then all of the content will be isolated by page/post/whatever (custom taxonomy FTW).
If you had the IDs ready and sorted plus the html/css ready to go I could drop this function in and work your one page to complete in under an hour. For production line work this type of helper is great -- also perfect for sites where you want a specific post/page to be placed in some weird way in some weird place outside of the loop.
If you have a problem w/script let me know, I have not used it in a few months, wrote it a few years ago...
I think you have a problem using the global $post and storing also the $post variable in the foreach loop from the menu objects. I think probably that's the cause of the error with the template part calling.
I would recommend you to remove the global $post declaration, the reset_postdata and also the setup_postdata since you aren't using the global in your loop, you're just naming it.
You're getting the $post object from the menu so it appears that you don't need the global or the other functions that are mostly used when you want to use "Loop Style" template tags without a post_id, like the_permalink() or the_title().
I ended up copying the default page.php template as page-page.php and loaded it like this:
$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? 'page' : $post->post_name;
Then in page.php just loaded page-page.php
Related
I'm developing a site for my company using wordpress+divi theme, this is hard customized due some booking system implementantations and personalized css, so I have made a child theme to avoid problems.
Now the question: Is there a way using my functions.php child file, to add a hook filter that can place content after a particular ID like
<li id="first-menu-item">home</li>
//my custom content through functions.php goes here
<li id="second-menu-item">portfolio</li>
<li id="third-menu-item">contacts</li>
I know that this is possible using jquery in this way
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery ( '#first-menu.item' ) .after( '<li>my custom content</li>' );
});
</script>
and this works but I think that this is not the right way cause what I want to place after that specific ID is note only html code but a wordpress search function and I think that It's wrong to think that I can place php tags inside a jquery.. so I have to add this
<?php get_search_form(); ?>
directly in my header.php but I want to avoid this cause the divi's develepors maybe can update the header.php file and I have to edit again my child header.php so I have to do this through my child function.php...I found that wordpress have different filter hook for making this like "wp_nav_menu_items" but what if I want to put code after a specific line/div/id/class?
This can be done using a custom helper function and adding filter on "wp_get_nav_menu_items". Following is the custom helper function you can use.
function _custom_nav_menu_item( $title, $url, $order, $parent = 0 ){
$item = new stdClass();
$item->ID = 1000000 + $order + parent;
$item->db_id = $item->ID;
$item->title = $title;
$item->url = $url;
$item->menu_order = $order;
$item->menu_item_parent = $parent;
$item->type = '';
$item->object = '';
$item->object_id = '';
$item->classes = array();
$item->target = '';
$item->attr_title = '';
$item->description = '';
$item->xfn = '';
$item->status = '';
return $item;
}
And following is how you can add filter.
add_filter( 'wp_get_nav_menu_items', 'your_custom_function', 20, 2 );
function your_custom_function( $items, $menu ){
//your logic and code for customization
return $items;
}
You will have define your own logic to identify right location. I have successfully used this with Divi theme.
Hope this will give you at least a starting point.
Well, I wonder though, why you would want to add search form in nav menus!
===== Update =====
You can customize wrapper of your menu to add something else. Please check this thread. This will be useful for you.
Also, to enable this functionality only for mobile version, you can use another plugin which detects mobile devices, mobble.
I have used this plugin many times on many websites and its results are quite reliable. You can conditionally enable the custom functionality using "is_mobile()" function from this plugin so you do not have to depend on css to hide the same on desktop devices.
really thank you for your reply, what I want to achive is to show after the last menu element in the only mobile version a search box, by default this is showed when you click an icon on top right, but I have a very strange wide logo and the languages flags that take up all the space , so I want to put the search box in a fullscreen mobile menu under the last element. I'm trying to understand your code and I'm just wondering if there is a way to put code after a specific div id in the header.php file but you are telling me to use the wp_get_nav_menu_items that is a filter for the navigation menu not for a class or id, I think that is little different from what I was asking for..I'm asking this also for other implementations
example
I'm creating a theme and i want the user to be able to use shortcodes.
Right now it outputs [the_shortcode] and I think I know why but don't know how to fix it.
I load the content of the page not in the conventional way.
Preferably the way is to load the_content() function. But the way my template is designed it loads content based upon placement in the hierarchy of pages.
So a parent has a different look then a child.
To do this I load the content with a foreach loop and echo out $grandchild->post_title. The page being a grandchild of a parent.
Now the way to fix this, according to the internet, is to use the apply_filters() function.
The function expects two parameters and I have no clue on how to fill them:
function apply_filters( $tag, $value )
This is my function for the shortcode:
function output_function(){
return 'Knees weak, arms are heavy';
}
add_shortcode('output', 'output_function');
The shortcode is placed in a page post like this: ['output']
Any thoughts on how to output page content through the filter?
What you want is the_content
$content = 'some string that has a [output] shortcode';
echo apply_filters('the_content', $content);
This filter will make sure all $content is parsed like the WordPress editor.
The same like the_content().
Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.
I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.
Ive started by adding a rewrite rule
RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]
Then page.php contains
require_once('../../../wp-blog-header.php');
get_header();
//custom php content
//get_sidebar(); // how to get this working.
get_footer();
This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.
In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.
Edit
In an attempt to dynamically inject content into an unknown theme ive done the following
global $post;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";
include get_template_directory()."/page.php";
This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.
Then u could try to load a specific template page in specific case.
function load_my_template( $template )
{
if( is_page() )
$template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";
return $template;
}
Or change the content that is use on loading page
function load_my_content( $content )
{
global $post;
$id = $post->ID;
if( is_page() )
{
ob_start();
include plugin_dir_path(__FILE__) . "dir_to/my_template.php";
$content = ob_get_clean();
}
return $content;
}
In your __construct()
add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );
Hope that help u.
Tell me if it's not corresponding with your question.
What is the first php file wordpress goes to to determine what page, document or URL to load. I have some PHP I want executed to determine what happens when certain URL's are entered. At the moment it's in the 404.php for want of a better place.
This however is not ideal. The index.php in theme is not referenced so what is?
It's one of the many pages loaded from wp-settings.php. Anyway, you don't have to go that deep into the core structure and spoil WP's native functionality I think, you can do whatever you prefer in theme functions.php, so you can get a page name just by doing something like this in functions.php:
$pagename = get_query_var('pagename');
if ( !$pagename && $id > 0 ) {
$post = $wp_query->get_queried_object();
$pagename = $post->post_name;
}
I use stackoverflow very often and just found out in google about this one, Nice :D
Well,
i want to save my every post i write from wordpress to my 3rd (OEXChangeble) website aswell at the same time, so i need to send the info to my website, developing a plugin iguess
I need basically the permalink (and i would be able to extract the rest of the params from my wesite), but better if i can get title, tags, permalink and description(or some of it)
i understand by my google research that all i need to do is add something like
<?php
//header of plugin
function myFunctionThatSendsMyWebsite($url){
// procedure containing a file_get_contents('myqwebsite?url=') request to my website
}
add_action('page_post', 'myFunctionThatSendsMyWebsite', $permalink));
?>
I have problems thoug finding the name of variables i have missing (marked by ???). I know that $post contains all objet, how to extract the info from it (if there is), or if it's complicated, it would be enought for me with permalink
Any tip?
Thanks!
according to this link, this should work, the post id will be sent to this function automatically and you can get anything you want from a post id.
function myFunctionToSendPost($post_ID) {
$post = get_post($post_ID);
$title = $post->post_title;
$content = $post->post_content;
$permalink = get_permalink($post_ID);
...
sendToYourServer($params);
return $post_ID;
}
add_action('publish_post', 'myFunctionToSendPost');
BTW, this function is called when a post is published, you can change it to happen when saved by
add_action('save_post', 'myFunctionToSendPost');
add these lines to your theme's functions.php file.
1) While this doesn't take advantage of the save_post feature, you can even use this code to display blog posts on a completely separate web site, as long as it’s on the same server and you have filesystem access to the WordPress directory on the original site. Simply modify the require() in the first block on this page to use the full path to your WordPress installation:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('/var/www/example.com/wordpress/wp-load.php');
query_posts('showposts=1');
?>
position your post with a while loop:
<?php while (have_posts()): the_post(); ?>
<?php endwhile; ?>
if you want to specify which parts of the post to display use this code:
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p>Read more...</p>
<?php endwhile; ?>
2) How about using the rss feeds from your wordpress blog?
The following code will display a list of your feed titles with descriptions including a hyperlink to the original WordPress posts:
<?php // Load the XML file into a Simple XML object
$filename = http://feeds.feedburner.com/<em>yourfeedname</em>";
$feed = simplexml_load_file($filename);
// Iterate through the list and create the unordered list
echo "<ul>";
foreach ($feed->channel->item as $item) {
echo "<li><a href='" . $item->link . "'>" . $item->title . "</a></li>";
}
echo "</ul>";
echo "<li>" . $item->description . "</li>";
?>
3) Feedburner has a free feature called BuzzBoost where you can get your posts to show in a regular HTML website that once activated you can simply copy the script that they provide into your HTML where you want the list to appear. From your feedburner account you can adjust some elements like whether the Blog title should appear or not, the format of the date, etc...
You can also style the output using regular CSS within you websites existing CSS.
Check it out Feedburner here:
https://www.google.com/accounts/ServiceLogin?service=feedburner&continue=http%3A%2F%2Ffeedburner.google.com%2Ffb%2Fa%2Fmyfeeds&gsessionid=5q8jqacBluH1-AnXp08ZFw
Are you trying to save a copy of the post somewhere else when they first publish the post? Or on every page view?
You can trigger it when the post is saved by writing a plugin that implements the save_post hook:
http://codex.wordpress.org/Plugin_API/Action_Reference
To do it on every page, you'd probably write a plugin with a filter hook on the page (if it's something you want other people to use) or if it's just one site, you could add it to your theme.
But... Are you sure you want to do this? It seems like your keepyourlinks site might be better implemented as an update service: http://codex.wordpress.org/Update_Services